package com.example.housemanage

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.housemanage.entity.HouseManageEntityItem
import com.example.housemanage.revise.ReviseActivity

class RoomAdapter(data: MutableList<HouseManageEntityItem>, private val onDeleteRoom: ((HouseManageEntityItem) -> Unit)? = null) :
    BaseQuickAdapter<HouseManageEntityItem, BaseViewHolder>(R.layout.item_room, data) {
    override fun convert(holder: BaseViewHolder, item: HouseManageEntityItem) {
        holder.setText(R.id.tv_room_number, "${item.id}室")
        holder.setText(R.id.tv_enterprise, "企业：${item.enterpriseName}")
        holder.setText(R.id.tv_area, "面积：${item.area}m²")
        val tvStatus = holder.getView<TextView>(R.id.tv_rent_status)
        if (item.rented) {
            tvStatus.text = "已租"
            tvStatus.setTextColor(0xFFFF5252.toInt()) // 红色
        } else {
            tvStatus.text = "出租"
            tvStatus.setTextColor(0xFF4CAF50.toInt()) // 绿色
        }
        
        // 添加点击事件，跳转到详情页面
        holder.itemView.setOnClickListener {
            val context = holder.itemView.context
            val intent = Intent(context, ReviseActivity::class.java)
            intent.putExtra("property_id", item.id)
            context.startActivity(intent)
        }
    }
    
    /**
     * 设置滑动删除功能
     * @param recyclerView 房间列表RecyclerView
     * @param context 上下文
     */
    fun setupSwipeDelete(recyclerView: RecyclerView, context: Context) {
        val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(
            0, ItemTouchHelper.LEFT
        ) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                val room = data[position]
                
                AlertDialog.Builder(context)
                    .setTitle("删除房间")
                    .setMessage("确定要删除 ${room.id}室 吗？\n企业：${room.enterpriseName}\n面积：${room.area}m²")
                    .setNegativeButton("取消") { _, _ ->
                        notifyItemChanged(position)
                    }
                    .setPositiveButton("确定") { _, _ ->
                        onDeleteRoom?.invoke(room)
                    }
                    .setOnCancelListener {
                        notifyItemChanged(position)
                    }
                    .show()
            }
        })
        itemTouchHelper.attachToRecyclerView(recyclerView)
    }
}